//
//  PKRootLeftViewController.m
//  PianKe
//
//  Created by lanou on 15/9/25.
//  Copyright (c) 2015年 李伟伟. All rights reserved.
//

#import "PKRootLeftViewController.h"
#import "PKRootRightViewController.h"
#import "HelpPCH.pch"
#import "PKRootLeftViewHeaderView.h"
#import "PKLoginViewController.h"
@interface PKRootLeftViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)PKRootRightViewController *rootRightVC;
@property(nonatomic,strong)UITableView *mainListTableView;//主列表
@property(nonatomic,strong)NSArray *dataSourceArray;//提供数据源
@property(nonatomic,strong)PKLoginViewController *loginVC;
@property(nonatomic,strong)UIButton *loginButton;
@end

@implementation PKRootLeftViewController

-(NSArray *)dataSourceArray
{
    if (!_dataSourceArray) {
        _dataSourceArray = @[@"1良品",@"2阅读",@"3社区",@"4电台"];
    }
    return _dataSourceArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加右侧视图控制器的View
    //ARC里面 ，编译器（LLVM)会自动帮我们添加release 代码 添加的时机就是这个对象走出作用域的时候，所以我们的工程被编译成二进制文件的时候还是有release代码的 只是我们没写 看不到
    
    PKRootLeftViewHeaderView *headerView = [[PKRootLeftViewHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WITDTH-40, 200)];
    headerView.backgroundColor = [UIColor redColor];
    [self.view addSubview:headerView];
    
    _loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _loginButton.frame = CGRectMake(200, 50, 50, 20);
    [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [_loginButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:_loginButton];
    
    
    
    _mainListTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 200, SCREEN_WITDTH-40, SCREEN_HEIGHT-200-80)];
    _mainListTableView.delegate = self;
    _mainListTableView.dataSource = self;
    [_mainListTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    _mainListTableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [self.view addSubview: _mainListTableView];
    
   _rootRightVC = [[PKRootRightViewController alloc]init];
    [self.view addSubview:_rootRightVC.view];
    
    
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeName) name:@"Login" object:nil];
    
    
    
    
}

-(void)changeName
{
    NSString *auth = [[NSUserDefaults standardUserDefaults] objectForKey:@"auth"];
    if (auth && ![auth isEqualToString:@""]) {
        [_loginButton setTitle:auth forState:UIControlStateNormal];
    }

    
}


-(void)login
{
    
    _loginVC = [[PKLoginViewController alloc]init];
    [self presentViewController:_loginVC animated:YES completion:nil];
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_rootRightVC changeViewWithIndex:indexPath.row];
    
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArray.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    cell.textLabel.text = self.dataSourceArray[indexPath.row];
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
